package com.meichis.mcsappframework.camera;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.DisplayMetrics;
import android.view.Surface;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CameraUtils {
    /**
     * 获取合适的size
     *
     * @param sizeList
     * @param height   高度
     * @return 相对于竖屏来说 最接近这个height高度的size
     */
    public static Size getProperSize(List<Size> sizeList, int height) {
        //主width降序，副heigth升序
        Collections.sort(sizeList, new Comparator<Size>() {
            @Override
            public int compare(Size lhs, Size rhs) {
                return lhs.width < rhs.width ? 1 : (lhs.width == rhs.width ?
                        (lhs.height < rhs.height ? -1 : (lhs.height == rhs.height ? 0 : 1)) : -1);
            }
        });
        Size result = sizeList.get(0);
        for (Size size : sizeList) {
            if ((size.width > height ? size.width - height : height - size.width) < (result.width > height ? result.width - height : height - result.width)) {
                result = size;
            }
        }
        return result;
    }

    public static int getPreviewDegree(Activity activity) {
        // 获得手机的方向
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        int degree = 0;
        // 根据手机的方向计算相机预览画面应该选择的角度
        switch (rotation) {
            case Surface.ROTATION_0:
                degree = 90;
                break;
            case Surface.ROTATION_90:
                degree = 0;
                break;
            case Surface.ROTATION_180:
                degree = 270;
                break;
            case Surface.ROTATION_270:
                degree = 180;
                break;
        }
        return degree;
    }

    public static DisplayMetrics GetMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

    /**
     * 开启闪光灯
     */
    public static boolean openF(Camera camera) {
        if (camera == null) {
            return false;
        }
        Camera.Parameters parameters = camera.getParameters();
        if (parameters == null) {
            return false;
        }
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes == null) {
            return false;
        }
        String flashMode = parameters.getFlashMode();
        // 检查是否存在相机闪光灯
        if (flashModes == null) {
            return false;
        }
        if (!Camera.Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
            if (flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(parameters);
                return true;
            }
        } else
            return true; //如果已经是开启状态也返回true
        return false;
    }

    /**
     * 关闭闪光灯
     */
    public static boolean stopF(Camera camera) {
        if (camera == null)
            return false;
        Camera.Parameters parameters = camera.getParameters();
        if (parameters == null)
            return false;
        List<String> flashModes = parameters.getSupportedFlashModes();
        String flashMode = parameters.getFlashMode();
        // 检查是否存在相机闪光灯
        if (flashModes == null)
            return false;
        if (!Camera.Parameters.FLASH_MODE_OFF.equals(flashMode)) {
            // 关掉闪光
            if (flashModes.contains(Camera.Parameters.FLASH_MODE_OFF)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(parameters);
                return true;
            }
        } else
            return true;  //如果已经是关闭状态也返回true
        return false;
    }
}
